package com.syedu.cels.util;

import java.io.InputStream;
import java.util.Properties;

/**
 * description：反射技术来创建类对象
 * @author Administrator
 * @date 2022/4/8 16:28
 */
public class BeanFactory {
    private static Properties properties = new Properties();

    static {
        try {
            InputStream in = Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream("bean.properties");
            properties.load(in);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Object getBean(String beanName) {
        Object obj = null;
        String className = properties.getProperty(beanName);
        try {
            //反射创建对象
            obj = Class.forName(className).getConstructor().newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
}